package com.usian.controller;

import com.usian.feign.ItemServiceFeign;

import com.usian.pojo.TbItem;
import com.usian.utils.PageResult;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/backend/item")
public class ItemController {
    @Autowired
    private ItemServiceFeign itemServiceFeign;

    /**
     * 查询商品基本信息
     */
    @RequestMapping("/selectItemInfo")
    public Result selectItemInfo(Long itemId) {
        TbItem tbItem = itemServiceFeign.selectItemInfo(itemId);
        if (tbItem != null) {
            return Result.ok(tbItem);
        }
        return Result.error("查无结果");
    }


    /**
     * shangxingming
     * 查询商品列表接口
     *
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/selectTbItemAllByPage")
    public Result selectTbItemAllByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "4") Integer rows) {
        PageResult pageResult = itemServiceFeign.selectTbItemAllByPage(page, rows);
        if (pageResult != null && pageResult.getResult() != null && pageResult.getResult().size() > 0) {
            return Result.ok(pageResult);
        }
        return Result.error("查询失败");
    }

    /**
     * 冯果果
     * 根据Id删除商品
     *
     * @param itemId
     */
    @RequestMapping(value = "/deleteItemById", method = RequestMethod.POST)
    public Result deleteItemById(Long itemId) {
        int res = itemServiceFeign.deleteByPrimaryKey(itemId);
        if (res == 0) {
            return Result.error("删除失败！");
        } else {
            return Result.ok();
        }
    }

    /**
     * 刘蕊
     * 商品添加接口
     *
     * @param tbItem
     * @param desc
     * @param itemParams
     * @return
     */
    @RequestMapping("/insertTbItem")
    public Result insertTbItem(TbItem tbItem, String desc, String itemParams) {
        Integer insertTbItemNum = itemServiceFeign.insertTbItem(tbItem, desc, itemParams);
        if (insertTbItemNum == 3) {
            return Result.ok();
        }
        return Result.error("添加失败");
    }

    /**
     * 根据itemId回显商品信息
     *
     * @param itemId
     * @return
     */
    @RequestMapping("/preUpdateItem")
    public Result preUpdateItem(Long itemId) {
        Map<String, Object> map = itemServiceFeign.preUpdateItem(itemId);
        if (map.size() > 0) {
            return Result.ok(map);
        }
        return Result.error("查无结果");
    }

    /**
     * 薛丁豪22.03.24
     * 修改商品信息地址
     * 修改的保存
     *
     * @param tbItem
     * @param desc
     * @param itemParams
     * @return
     */
    @RequestMapping("/updateTbItem")
    public Result updateTbItem(TbItem tbItem, String desc, String itemParams) {
        try {
            itemServiceFeign.updateTbItem(tbItem, desc, itemParams);
            return Result.ok();
        } catch (Exception e) {
            return Result.error("修改失败");
        }
    }

}